/**
 * @projectName CollegeEmploymentNetwork
 * @package com.kzj.CollegeEmploymentNetwork.shiro.server.impl
 * @className com.kzj.CollegeEmploymentNetwork.shiro.server.impl.UserService
 * @copyright Copyright 2025 Thunisoft, Inc All rights reserved.
 */
package com.kzj.CollegeEmploymentNetwork.shiro.server.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;

import com.kzj.CollegeEmploymentNetwork.shiro.mapper.PermissionsMapper;
import com.kzj.CollegeEmploymentNetwork.shiro.mapper.RoleMapper;
import com.kzj.CollegeEmploymentNetwork.shiro.mapper.RolePermissionsMapper;
import com.kzj.CollegeEmploymentNetwork.shiro.mapper.UserMapper;
import com.kzj.CollegeEmploymentNetwork.shiro.mapper.UserPermissionsMapper;
import com.kzj.CollegeEmploymentNetwork.shiro.mapper.UserRoleMapper;
import com.kzj.CollegeEmploymentNetwork.shiro.pojo.po.Permissions;
import com.kzj.CollegeEmploymentNetwork.shiro.pojo.po.Role;
import com.kzj.CollegeEmploymentNetwork.shiro.pojo.po.RolePermissions;
import com.kzj.CollegeEmploymentNetwork.shiro.pojo.ShiroUser;
import com.kzj.CollegeEmploymentNetwork.shiro.pojo.po.User;
import com.kzj.CollegeEmploymentNetwork.shiro.pojo.po.UserPermissions;
import com.kzj.CollegeEmploymentNetwork.shiro.pojo.po.UserRole;
import com.kzj.CollegeEmploymentNetwork.shiro.server.IUserService;

/**
 * UserService
 * @description
 * @author KZJ
 * @date 2025/1/24 13:26
 * @version TODO
 */
@Primary
@Service("shiroUserService")
public class UserService implements IUserService {
    @Autowired
    UserMapper userMapper;
    @Autowired
    PermissionsMapper permissionsMapper;
    @Autowired
    RoleMapper roleMapper;
    @Autowired
    RolePermissionsMapper rolePermissionsMapper;
    @Autowired
    UserRoleMapper userRoleMapper;
    @Autowired
    UserPermissionsMapper userPermissionsMapper;
    @Override
    public Set<String> queryRolesByUserId(String userId) {
        UserRole userRole = new UserRole();
        userRole.setUserId(userId);
        List<UserRole> select = userRoleMapper.select(userRole);
        Set<Integer> roleIds = select.stream().map(UserRole::getRoleId).collect(Collectors.toSet());
        List<Role> roles = roleIds.isEmpty()?new ArrayList<>():roleMapper.batchSelectByPrimaryKey(roleIds);
        Set<String> result = roles.stream().map(Role::getName).collect(Collectors.toSet());
        return result;
    }

    @Override
    public Set<String> queryPermissionsByUserId(String userId) {
        UserPermissions userPermissions=new UserPermissions();
        userPermissions.setUserId(userId);
        List<UserPermissions> userPermissionsList = userPermissionsMapper.select(userPermissions);
        Set<Integer> permissionsIds = userPermissionsList.stream().map(UserPermissions::getPermissionsId).collect(Collectors.toSet());
        UserRole userRole = new UserRole();
        userRole.setUserId(userId);
        List<UserRole> select = userRoleMapper.select(userRole);
        Set<Integer> roleIds = select.stream().map(UserRole::getRoleId).collect(Collectors.toSet());
        List<RolePermissions> rolePermissionsList = roleIds.isEmpty()?new ArrayList<>():rolePermissionsMapper.selectByRoleIds(new ArrayList<>(roleIds));
        Set<Integer> permissionsIds2 = rolePermissionsList.stream().map(RolePermissions::getPermissionsId).collect(Collectors.toSet());
        permissionsIds.addAll(permissionsIds2);
        List<Permissions> permissions = permissionsIds.isEmpty()?new ArrayList<>():permissionsMapper.batchSelectByPrimaryKey(permissionsIds);
        Set<String> result = permissions.stream().map(Permissions::getName).collect(Collectors.toSet());
        return result;
    }

    @Override
    public ShiroUser queryShiroUserByUsername(String username) {
        User user=queryUserByUsername(username);
        if(user==null)
            return null;
        ShiroUser shiroUser=new ShiroUser();
        BeanUtils.copyProperties(user,shiroUser);
        shiroUser.setRoles(queryRolesByUserId(user.getId()));
        shiroUser.setPermissions(queryPermissionsByUserId(user.getId()));
        return shiroUser;
    }
    @Override
    public User queryUserByUsername(String username) {
        User user = new User();
        user.setUsername(username);
        user=userMapper.selectOne(user);
        return user;
    }
}